package com.lsb.dao;

import com.lsb.pojo.RegionStats;
import com.lsb.vo.ChartVO;
import com.lsb.vo.StateChartVO;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：liusibo
 * @description：dao层获取数据
 * @date ：2021/4/27 17:22
 */
@Repository
public class DataDao {
    //前端表格数据
    private List<RegionStats> statsList = new ArrayList<>();
    //前端折线图数据：全世界2020全年数据增长图
    private List<ChartVO> chartVOList = new ArrayList<>();
    //前端折线图数据：各地区2020全年数据增长图
    private List<StateChartVO> stateChartVOList = new ArrayList<>();

    @Autowired
    private RestTemplate restTemplate;

    //每天6:00:00定时执行执行该方法
    @Scheduled(cron = "0 0 6 * * *")
    public void pullData(){
        //获取新冠病毒统计数据的csv文件二进制流
        String data_url =
                "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        RequestEntity<Void> requestEntity = RequestEntity
                .get(data_url)
                .headers(httpHeaders -> httpHeaders.add("User-Agent","liusibo"))
                .build();
        ResponseEntity<Resource> exchange = restTemplate.exchange(requestEntity,Resource.class);
        Resource body = exchange.getBody();
        Reader in = null;
        try {
            in = new InputStreamReader(body.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //解析CSV文件
        Iterable<CSVRecord> records = null;
        try {
            records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将每行数据添加到statsList中
        int[] num = {0,0,0,0,0,0,0,0,0,0,0};
        int i = 0;
        for (CSVRecord record : records) {
            StateChartVO stateChartVO = new StateChartVO();
            List<String> dateList = new ArrayList<>();
            List<Integer> numList = new ArrayList<>();
            dateList.add("Jan");
            dateList.add("Feb");
            dateList.add("Mar");
            dateList.add("Apr");
            dateList.add("May");
            dateList.add("June");
            dateList.add("July");
            dateList.add("Aug");
            dateList.add("Sep");
            dateList.add("Oct");
            dateList.add("Nov");
            numList.add(Integer.parseInt(record.get("1/22/20")));
            numList.add(Integer.parseInt(record.get("2/22/20")));
            numList.add(Integer.parseInt(record.get("3/22/20")));
            numList.add(Integer.parseInt(record.get("4/22/20")));
            numList.add(Integer.parseInt(record.get("5/22/20")));
            numList.add(Integer.parseInt(record.get("6/22/20")));
            numList.add(Integer.parseInt(record.get("7/22/20")));
            numList.add(Integer.parseInt(record.get("8/22/20")));
            numList.add(Integer.parseInt(record.get("9/22/20")));
            numList.add(Integer.parseInt(record.get("10/22/20")));
            numList.add(Integer.parseInt(record.get("11/22/20")));
            stateChartVO.setDateList(dateList);
            stateChartVO.setNumList(numList);

            num[0] += Integer.parseInt(record.get("1/22/20"));
            num[1] += Integer.parseInt(record.get("2/22/20"));
            num[2] += Integer.parseInt(record.get("3/22/20"));
            num[3] += Integer.parseInt(record.get("4/22/20"));
            num[4] += Integer.parseInt(record.get("5/22/20"));
            num[5] += Integer.parseInt(record.get("6/22/20"));
            num[6] += Integer.parseInt(record.get("7/22/20"));
            num[7] += Integer.parseInt(record.get("8/22/20"));
            num[8] += Integer.parseInt(record.get("9/22/20"));
            num[9] += Integer.parseInt(record.get("10/22/20"));
            num[10] += Integer.parseInt(record.get("11/22/20"));

            RegionStats regionStats = new RegionStats();
            int j= i;
            regionStats.setId(j);
            stateChartVO.setId(j);
            i++;
            //添加地区
            regionStats.setState(record.get("Province/State"));
            stateChartVO.setState(record.get("Province/State"));
            //添加国家
            regionStats.setCountry(record.get("Country/Region"));
            stateChartVO.setCountry(record.get("Country/Region"));
            //最后一列数据，即最新病例数
            int latesttotal = Integer.parseInt(record.get(record.size() - 1));
            //倒数第二列数据，即前一天的病例数
            int thedaybefore = Integer.parseInt(record.get(record.size() - 2));
            //添加总病例数
            regionStats.setLatestTotalCases(latesttotal);
            //添加新增病例数
            regionStats.setDiffFromPrevDay(latesttotal - thedaybefore);
            statsList.add(regionStats);
            stateChartVOList.add(stateChartVO);
        }
        System.out.println(statsList);
        System.out.println(stateChartVOList);

        chartVOList.add(new ChartVO("Jan",num[0]));
        chartVOList.add(new ChartVO("Feb",num[1]));
        chartVOList.add(new ChartVO("Mar",num[2]));
        chartVOList.add(new ChartVO("Apr",num[3]));
        chartVOList.add(new ChartVO("May",num[4]));
        chartVOList.add(new ChartVO("June",num[5]));
        chartVOList.add(new ChartVO("July",num[6]));
        chartVOList.add(new ChartVO("Aug",num[7]));
        chartVOList.add(new ChartVO("Sept",num[8]));
        chartVOList.add(new ChartVO("Oct",num[9]));
        chartVOList.add(new ChartVO("Nov",num[10]));

        System.out.println(chartVOList);

    }
    public List<RegionStats> getStatsList() {
        return statsList;
    }

    public List<ChartVO> getChartVOList() {
        return chartVOList;
    }

    public List<StateChartVO> getStateChartVOList() {
        return stateChartVOList;
    }
}
